﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Nexus.IPConfiguration.Configurator.NetworkAdapterConfigs;

namespace Nexus.IPConfiguration.Configurator.UserControls
{
    public partial class NetworkAdapterConfigUserControl : UserControl
    {
        #region fields
        private NetworkAdapterConfiguration config;
        #endregion

        #region properties
        public NetworkAdapterConfiguration NetworkAdapterConfig
        {
            get
            {
                return config;
            }
            set
            {
                if (value != null)
                {
                    config = value;
                    BindData();
                    Enabled = true;
                }
                else
                {
                    Enabled = false;
                    ResetToDefault();
                }
            }
        }
        #endregion

        public NetworkAdapterConfigUserControl()
        {
            InitializeComponent();
            Enabled = false;
        }

        public NetworkAdapterConfigUserControl(NetworkAdapterConfiguration config)
        {
            InitializeComponent();
            this.config = config;
            if (this.config != null)
            {
                BindData();
                Enabled = true;
            }
            else
            {
                Enabled = false;
                ResetToDefault();
            }
        }

        private void BindData()
        {
            bindingSourceNetworkAdapterConfiguration.DataSource = config;
            bindingSourceNetworkAdapterConfiguration.ResetBindings(false);
            checkBox1_CheckedChanged(chkIP, new EventArgs());
            chkDNS_CheckedChanged(chkDNS, new EventArgs());
            chkProxy_CheckedChanged(chkProxy, new EventArgs());
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chk = sender as CheckBox;
            SetIpState(chk.Checked);
        }

        private void SetIpState(bool state)
        {
            txtDefaultGateway.Enabled = !state;
            txtIPAddress.Enabled = !state;
            txtSubnetMask.Enabled = !state;
        }

        private void chkDNS_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chk = sender as CheckBox;
            SetDnsState(chk.Checked);
        }

        private void SetDnsState(bool state)
        {
            txtDNS1.Enabled = !state;
            txtDNS2.Enabled = !state;
        }

        private void chkProxy_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chk = sender as CheckBox;
            SetProxyState(chk.Checked);
        }

        private void SetProxyState(bool state)
        {
            txtOverride.Enabled = state;
            txtPort.Enabled = state;
            txtProxyAddress.Enabled = state;
        }

        private void txtPort_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsDigit((char)e.KeyChar) == false)
            {
                e.Handled = true;
            }
        }

        private void ResetToDefault()
        {
            Controls.Cast<Control>().Where(c => c is CheckBox).Cast<CheckBox>().ToList().ForEach(c => c.Checked = false);
            Controls.Cast<Control>().Where(c => c is TextBox).Cast<TextBox>().ToList().ForEach(t => t.Text = string.Empty);
        }
    }
}
